package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.ReportDao;
import com.itheima.dao.SetmealDao;
import com.itheima.pojo.ReportPackage;
import com.itheima.service.ReportService;
import com.itheima.utils.AgeUtils;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 运营数据统计服务
 */
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ReportDao reportDao;

    //查询运营数据
    public Map<String, Object> getBusinessReportData() throws Exception {
        //报表日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //获得本周一日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获得本月第一天日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //本日新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(today);
        //总会员数
        Integer totalMember = memberDao.findMemberTotalCount();
        //本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
        //本月新增会员数
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);

        //今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
        //本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);
        //本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);
        //今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
        //本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);
        //本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);

        //热门套餐查询
        List<Map> hotSetmeal = orderDao.findHotSetmeal();

        Map<String, Object> result = new HashMap<>();
        result.put("reportDate", today);
        result.put("todayNewMember", todayNewMember);
        result.put("totalMember", totalMember);
        result.put("thisWeekNewMember", thisWeekNewMember);
        result.put("thisMonthNewMember", thisMonthNewMember);
        result.put("todayOrderNumber", todayOrderNumber);
        result.put("thisWeekOrderNumber", thisWeekOrderNumber);
        result.put("thisMonthOrderNumber", thisMonthOrderNumber);
        result.put("todayVisitsNumber", todayVisitsNumber);
        result.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        result.put("hotSetmeal", hotSetmeal);
        return result;
    }

    //套餐收入统计查询
    public List<Map<String, Object>> findSetmealIncomeByDate(String startDate, String endDate) {
        List<ReportPackage> setmealIncomeByDate = reportDao.findSetmealIncomeByDate(startDate, endDate);

        Map<String, Object> map = null;
        List<Map<String, Object>> list = new ArrayList<>();

        //设置一个记录总收入的变量
        Integer totalCount = 0;

        for (ReportPackage reportPackage : setmealIncomeByDate) {
            map = new HashMap<>();
            map.put("name", reportPackage.getName());
            map.put("value", reportPackage.getPriceCount());
            //每记录一个套餐收入便加入列表中
            list.add(map);
            totalCount = totalCount + reportPackage.getPriceCount();
        }

        //new一个新map以免覆盖之前数据
        map = new HashMap<>();
        map.put("name", "总收入");
        map.put("value", totalCount);
        list.add(map);

        return list;
    }

    //预约到诊统计查询
    public Map<String, Object> getOrderReportByDate(String startDate, String endDate) throws Exception {
        //查询日期
        List<String> dateRange = reportDao.findDateByDate(startDate, endDate);
        //查询每日预约数
        List<Integer> countOrder = reportDao.findCountOrderByDate(startDate, endDate);
        //查询每日就诊数
        List<Map<String, Object>> tempDatedCount = reportDao.findHaveDatedCountByDate(startDate, endDate);

        //重新封装一个对象，便于封装数据
        Map<String, Object> aMap = new HashMap<>();

        for (Map<String, Object> map : tempDatedCount) {
            String date = DateUtils.parseDate2String((Date) map.get("orderDate"));
            aMap.put(date, map.get("VALUE"));
        }

//        System.out.println(datedCount);
//        System.out.println(aMap);

        List<Object> haveDatedCount = new ArrayList<>();

        //将不存在就诊数的时日设为0
        for (int i = 0; i < dateRange.size(); i++) {
            String tempDate = dateRange.get(i);
            if (aMap.get(tempDate) != null) {
                //使前端的日期与就诊数能相互对应
                haveDatedCount.add(i, aMap.get(tempDate));
            } else {
                haveDatedCount.add(i, 0);
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("dateRange", dateRange);
        map.put("countOrder", countOrder);
        map.put("haveDatedCount", haveDatedCount);

        return map;
    }

    //年龄分布查询
    public List<Integer> getMemberAge() throws Exception {
        List<Date> birthDayList = reportDao.findBirthday();
        int kidCount = 0;   //婴幼儿0-6岁
        int childCount = 0; //少儿7-12岁
        int teenCount = 0;  //青少年13-17岁
        int youthCount = 0; //青年18-45岁
        int midCount = 0;   //中年46-69岁
        int oldCount = 0;   //老年>69岁
        for (Date date : birthDayList) {
            if (date == null) continue;
            int age = AgeUtils.getAge(date);
            if (age >= 0 && age <= 6) {
                kidCount++;
            } else if (age >= 7 && age <= 12) {
                childCount++;
            } else if (age >= 13 && age <= 17) {
                teenCount++;
            } else if (age >= 18 && age <= 45) {
                youthCount++;
            } else if (age >= 45 && age <= 69) {
                midCount++;
            } else if (age >= 69) {
                oldCount++;
            }
        }

        List<Integer> list = new ArrayList<>();
        list.add(kidCount);
        list.add(childCount);
        list.add(teenCount);
        list.add(youthCount);
        list.add(midCount);
        list.add(oldCount);

        return list;
    }

}
